<?php


namespace api\modules\app\services;


use api\components\BaseController;
use api\components\Common;
use api\modules\app\models\MetaArea;
use api\modules\app\models\Qrcode;
use api\modules\app\models\QrcodeBind;
use api\modules\app\models\Unit;
use yii\helpers\ArrayHelper;

class QrcodeBindService
{
    public function list($params)
    {
        $data = $this->search($params);
        $count = $this->search($params, true);
        foreach ($data as &$v) {
            $v['qrcode_info'] = Qrcode::find()->where(['code' => $v['qrcode']])->asArray()->one();
            $unitInfo = Unit::find()->where(['unit_code' => $v['unit_code']])->asArray()->one();
            $v['unit_info'] = $this->transferUnit($unitInfo);
        }
        $result['count'] = $count;
        $result['data'] = $data;

        return $result;
    }

    public function search($params, $count = false)
    {
        $query = QrcodeBind::find();
        $query->andWhere(['is_delete' => 0]);
        if (isset($params['qrcode']) && !empty($params['qrcode'])) {
            $query->andWhere(['qrcode' => $params['qrcode']]);
        }

        if (isset($params['unit_code']) && !empty($params['unit_code'])) {
            $query->andWhere(['unit_code' => $params['unit_code']]);
        }

        if (isset($params['type']) && !empty($params['type'])) {
            $query->andWhere(['type' => $params['type']]);
        }

        if ($count) {
            return $query->count();
        }

        $query = Common::constructPage($query, $params);
        return $query->asArray()->all();
    }

    public function transferUnit($row)
    {
        $metas = MetaArea::find()->select(['name', 'code'])->andFilterWhere(['category' => ['区', '街道'], 'status' => 1])->asArray()->all();
        $metaMaps = ArrayHelper::map($metas, 'code', 'name');

        if (!empty($row)) {
            $row['district_name'] = $row['meta_level3_name'] = $metaMaps[$row['district_code']];
            $row['street_name'] = $row['meta_level4_name'] = $metaMaps[$row['street_code']];
            $row['meta_level5_name'] = $row['community_name'];
            $row['meta_level6_name'] = $row['estate_name'];
            $row['iot_code'] = explode(',', $row['iot_code']);
            $row['meta_level1'] = 'sh01';
            $row['meta_level2'] = 'sh0101';
            $row['meta_level3'] = $row['district_code'];
            $row['meta_level4'] = $row['street_code'];
            $row['meta_level5'] = $row['community_code'];
            $row['meta_level6'] = $row['estate_code'];
        }
        return $row;
    }

    public function cityList($params)
    {
        $unitInfo = Unit::find()->where(['type' => Unit::TYPE_UNIT, 'qrcode' => $params['qrcode']])->asArray()->one();
        if (empty($unitInfo)) {
            return ['data' => [], 'count' => 0];
        }
        $data[0]['type'] = Unit::TYPE_UNIT;
        $data[0]['unit_info'] = $this->transferUnit($unitInfo);;
        $result['data'] = $data;
        $result['count'] = 1;
        return $result;
    }

}
